<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\EnName;
use app\common\model\NameZi;
use app\common\model\SanCai;
use app\common\model\Xing;
use app\common\model\XingName;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use think\facade\View;

class Office extends AdminBase
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    // 导出
    public function export() {

    }

    // 导入
    public function importXing() {
        if ($this->request->isPost()) {
            $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

            if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {

                $arr_file = explode('.', $_FILES['file']['name']);
                $extension = end($arr_file);

                if('csv' == $extension) {
                    $reader = new Csv();
                } else {
                    $reader = new Xlsx();
                }

                $spreadsheet = $reader->load($_FILES['file']['tmp_name']);
                $sheetData = $spreadsheet->getActiveSheet()->ToArray();
                foreach ($sheetData as $key=>$item) {
                    if ($key != 0) {
                        Xing::create([
                            "title" => $item[0],
                            "hua" => $item[1],
                            "wu" => $item[2],
                            "attr1" => $item[3],
                            "attr2" => $item[4],
                            "attr3" => $item[5],
                            "attr4" => $item[6],
                            "attr5" => $item[7],
                        ]);
                    }
                }
            }
            return 1;

        }
        return  View::fetch();
    }

    // 导入名字
    public function importName() {
        if ($this->request->isPost()) {
            $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

            if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {

                $arr_file = explode('.', $_FILES['file']['name']);
                $extension = end($arr_file);

                if('csv' == $extension) {
                    $reader = new Csv();
                } else {
                    $reader = new Xlsx();
                }

                $spreadsheet = $reader->load($_FILES['file']['tmp_name']);
                $sheetData = $spreadsheet->getActiveSheet()->ToArray();
                foreach ($sheetData as $key=>$item) {
                    if ($key != 0) {
                        if (!empty($item[0])) {
                            NameZi::create([
                                "title" => $item[0],
                                "hua" => $item[1],
                                "wu" => $item[2],
                                "cheng" => $item[3],
                                "shi" => $item[4],
                                "ci" => $item[5],
                                "ming" => $item[6],
                                "zi" => $item[7],
                                "sex" => 2
                            ]);
                        }
                    }
                }
            }
            return 1;

        }
        return  View::fetch();
    }


    // 导入三才
    public function sanCaiList()
    {
        $file_path = "san.xlsx";

        $reader = new Xlsx();
        $spreadsheet = $reader->load($file_path);
        $sheetData = $spreadsheet->getActiveSheet()->ToArray();
        $data=[];
        foreach ($sheetData as $key=>$item) {
                $temp["shu"] = [str_replace(" ","",$item[0]),str_replace(" ","",$item[1])];
                $temp["wu"] = str_replace(" ","",$item[2]);
                $data[] = $temp;
                SanCai::create([
                    "shu" => str_replace(" ","",$item[0]),
                    "wu" => $item[2],
                    "han" => $item[3]
                ]);
                SanCai::create([
                    "shu" => str_replace(" ","",$item[1]),
                    "wu" => $item[2],
                    "han" => $item[3]
                ]);
        }

        dump($data);
    }

    // 导入五格
    public function wuge()
    {
        $list = Xing::order('hua')->whereNotNull('attr1')->select()->toArray();
        $parent = "/(\d+\+\d+\+\d+).*?(天格(\d+)\((.*)\)).*?(人格(\d+)\((.*)\)).*?(地格(\d+)\((.*)\)).*?(总格(\d+)\((.*)\)).*?(外格(\d+)\((.*)\)).*?(天才(\d+)\((.*)\)).*?(人才(\d+)\((.*)\)).*?(地才(\d+)\((.*)\))/";
        $parent2 = "/(\d+\+\d+).*?(天格(\d+)\((.*)\)).*?(人格(\d+)\((.*)\)).*?(地格(\d+)\((.*)\)).*?(总格(\d+)\((.*)\)).*?(外格(\d+)\((.*)\)).*?(天才(\d+)\((.*)\)).*?(人才(\d+)\((.*)\)).*?(地才(\d+)\((.*)\))/";

//        $test = $list[0]['attr4'];
//        $list = preg_match($parent2,$test,$all);
//        dump($all);
//        return 1;
        foreach ($list as $item) {
            for ($i = 1;$i<=5;$i++) {
                if ($i>3) {
                    $list = preg_match($parent2,$item["attr{$i}"],$all);
                    if ($all) {
                        XingName::create([
                            "hua" => $item["hua"],
                            "zu_he" => $all[1],
                            "tian_ge" => "{$all[3]}|{$all[4]}",
                            "ren_ge" => "{$all[6]}|{$all[7]}",
                            "di_ge" => "{$all[9]}|{$all[10]}",
                            "zong_ge" => "{$all[12]}|{$all[13]}",
                            "wai_ge" => "{$all[15]}|{$all[16]}",
                            "tian_cai" => "{$all[18]}|{$all[19]}",
                            "ren_cai" => "{$all[21]}|{$all[22]}",
                            "di_cai" => "{$all[24]}|{$all[25]}",
                            "san_cai" => "{$all[18]}{$all[21]}{$all[24]}",
                            "type" => 1
                        ]);
                    }
                } else {
                    $list = preg_match($parent,$item["attr{$i}"],$all);
                    if ($all) {
                        XingName::create([
                            "hua" => $item["hua"],
                            "zu_he" => $all[1],
                            "tian_ge" => "{$all[3]}|{$all[4]}",
                            "ren_ge" => "{$all[6]}|{$all[7]}",
                            "di_ge" => "{$all[9]}|{$all[10]}",
                            "zong_ge" => "{$all[12]}|{$all[13]}",
                            "wai_ge" => "{$all[15]}|{$all[16]}",
                            "tian_cai" => "{$all[18]}|{$all[19]}",
                            "ren_cai" => "{$all[21]}|{$all[22]}",
                            "di_cai" => "{$all[24]}|{$all[25]}",
                            "san_cai" => "{$all[18]}{$all[21]}{$all[24]}",
                            "type" => 2
                        ]);
                    }
                }
            }
        }

        return 1;
    }

    public function enName()
    {
        $file_path = "na.txt";
        if(file_exists($file_path)) {
            $fp = fopen($file_path, "r");
            $str = fread($fp, filesize($file_path));//指定读取大小，这里把整个文件内容读取出来
            fclose($fp);
            $str = str_replace("\r\n", "|", $str);
            $str = iconv('gbk','UTF-8',$str);
            $str = explode("|",$str);
            $test = $str[4];
            $parent = "/([A-Za-z]+)(.*)/";

            foreach ($str as $k=>$item) {
                if ($k>1) {
                    preg_match($parent,$item,$all);
                    if ($all) {
                        EnName::create([
                            'title' => $all[1],
                            'shiyi' => $all[2],
                            'type' => 1
                        ]);
                    }
                }
            }
        }
    }
}